$(document).ready(function() {
	init(1);
});
	
//默认加载  
function init(pageNo){
	//获取信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/areaDetail/jpa/page?pageNum="+pageNo+"&pageSize="+pageSize,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		async: false,
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){	
					var id = n.id;
					var area=getarea(n.areaCode);
					var thislist = 
					'	<tr>'+				
					'		<td>'+(i+1)+'</td>'+
					'		<td>'+area.province+'</td>'+
					'		<td>'+area.city+'</td>'+
					'		<td>'+area.area+'</td>'+
					'		<td>'+n.detailPlace+'</td>'+
					'		<td>'+
 					'           <a class="btn btn-primary btn-xs" href="javascript:;" onclick="deleteeach('+id+')">删除</a>'+						
					'		</td>'+							
					'	</tr>';
					$('#thislist').append(thislist);														
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});
}
//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber); 
}

//删除
function deleteeach(id){
	if(confirm("确定要删除吗？")) {
		deletegq(id);
	}
}

function deletegq(id){
	$.ajax({
		url: urlcore + "/api/areaDetail/deleteAreaDetailOne?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				location.reload();
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

function getarea(areaCode){
	var da='';
	$.ajax({
		url: urlcore + "/api/openArea/findByAreaCode?areaCode="+areaCode,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		async: false,
		success:function(data){
			if (data.success == true) {
				 da = data.data;
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
	return da;
}
